<template>
<el-container class="home-container">
  <el-header>
    <div>
      <img src="../assets/heima.png">
      <span>宠物管理系统</span>
    </div>
    <el-button type="info" @click="unlogin">退出</el-button>
    </el-header>
  <el-container>
    <el-aside :width="isCollapse?'64px':'200px'">
       <div class="toggle-button"  @click="toggleCollapse">|||</div>
         <el-menu
         :router="true"
         :collapse-transition="false"
         :collapse="isCollapse"
         :unique-opened="true"
         :default-active="activePath"
        background-color="#333744"
        text-color="white"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose">
      
      <el-submenu v-for="item in menuList" :key=item.id :index="item.id+''">
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
          <el-menu-item v-for="subitem in item.children" :key="subitem.id"
           :index="'/'+subitem.path" @click="saveNavState('/' + subitem.path)">
            <i class="el-icon-menu"></i>
            <span>{{subitem.authName}}</span>
           </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main><router-view></router-view></el-main>
  </el-container>
</el-container>
</template>

<script>
import request from '../api/index'
export default {
name:'Home',
data(){
  return {
    menuList:[
       {
            id: "125",
            authName: "用户管理",
            path: "users",
            children: [
                {
                    id: "110",
                    authName: "用户列表",
                    path: "users",
                    children: [],
                    order: null
                }
            ],
            order: 1
        },
        {
            id: "103",
            authName: "权限管理",
            path: "rights",
            children: [
                {
                    id: "111",
                    authName: "角色列表",
                    path: "roles",
                    children: [],
                    order: null
                },
                {
                    id: "112",
                    authName: "权限列表",
                    path: "rights",
                    children: [],
                    order: null
                }
            ],
            order: 2
        },
          {
            id: "105",
            authName: "宠物管理",
            path: "pets",
            children: [
                {
                    id: "104",
                    authName: "宠物列表",
                    path: "pets",
                    children: [],
                    order: 1
                },
                {
                    id: "115",
                    authName: "宠物品种",
                    path: "type",
                    children: [],
                    order: 2
                },
               
            ],
            order: 2
        },
         {
            id: "106",
            authName: "医美管理",
            path: "medics",
            children: [
                {
                    id: "104",
                    authName: "项目列表",
                    path: "medics",
                    children: [],
                    order: 1
                },
                {
                    id: "115",
                    authName: "医美记录",
                    path: "records",
                    children: [],
                    order: 2
                },
               
            ],
            order: 2
        },
        {
            id: "101",
            authName: "商品管理",
            path: "goods",
            children: [
                {
                    id: "104",
                    authName: "商品列表",
                    path: "goods",
                    children: [],
                    order: 1
                },
                {
                    id: "115",
                    authName: "分类参数",
                    path: "params",
                    children: [],
                    order: 2
                },
                {
                    id: "121",
                    authName: "商品分类",
                    path: "categories",
                    children: [],
                    order: 3
                }
            ],
            order: 3
        },
        {
            id: "102",
            authName: "订单管理",
            path: "orders",
            children: [
                {
                    id: "107",
                    authName: "订单列表",
                    path: "orders",
                    children: [],
                    order: null
                }
            ],
            order: 4
        },
         {
            id: "107",
            authName: "库存管理",
            path: "stock",
            children: [
                {
                    id: "104",
                    authName: "库存列表",
                    path: "stock",
                    children: [],
                    order: 1
                },
                {
                    id: "115",
                    authName: "进货清单",
                    path: "intro",
                    children: [],
                    order: 2
                },
               
            ],
            order: 2
        },
        {
            id: "145",
            authName: "数据统计",
            path: "reports",
            children: [
                {
                    id: "146",
                    authName: "数据报表",
                    path: "reports",
                    children: [],
                    order: null
                }
    ],
       iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse:false,
      activePath:''

      }
  ],
  iconsObj:{
    '125':'iconfont icon-user',
    '103':'iconfont icon-tijikongjian',
    '101':'iconfont icon-shangpin',
    '102':'iconfont icon-danju',
    '145':'iconfont icon-baobiao',
    '105':'iconfont icon-shangpin',
    '106':'iconfont icon-shangpin',
    '107':'iconfont icon-danju'
  }
}
},
created(){
  // this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
},
methods:{
    unlogin(){
        window.sessionStorage.clear();
        this.$router.push('/login')
    },
  async  getMenuList(){
    try{ const {data:res}=await request({method:'get',url:'/menus'})
      if(res.meta.status!==200){
       return  this.$message.error(res.meta.msg)
      }
      else
        this.menuList=res.data
        console.log(res)}
        catch(err){
          this.$message.error(err)
        }

    },
    toggleCollapse(){
      this.isCollapse=!this.isCollapse
    },
    saveNavState(activePath){
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
        console.log(this.activePath)
    }

}
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>